<template>
  <div class="article-container">
    <!-- 过滤条件 -->
    <el-card class="filter-card" :body-style="{ padding: '20px 0 0 0' }">
      <div slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- card body -->
      <el-form ref="form" :model="filter" label-width="60px" size="mini">
        <el-form-item label="状态">
          <el-radio-group v-model="filter.status">
            <el-radio
              v-for="item in articleStatus"
              :key="item.id"
              :label="item.id"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select v-model="filter.channel_id" placeholder="请选择频道">
            <el-option
              v-for="channel in channels"
              :key="channel.id"
              :label="channel.name"
              :value="channel.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="filter.date"
            value-format="yyyy-MM-dd"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['12:00:00']"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 过滤结果/列表 -->
    <el-card class="result-card">
      <div slot="header">
        根据筛选条件共查询到 {{ total }} 条结果，当前是第{{ curPage }}页
      </div>
      <!-- card body -->
      <el-table :data="articles" style="width: 100%">
        <el-table-column prop="cover" label="封面" width="180">
          <template slot-scope="table">
            <el-image
              style="width: 120px; height: 100px"
              :src="
                table.row.cover.images[0] ||
                'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg'
              "
            >
              <!-- 加载提示 -->
              <div slot="placeholder" class="image-slot">
                加载中<span class="dot">...</span>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" width="180">
        </el-table-column>
        <el-table-column prop="status" label="状态" width="180">
          <template slot-scope="scope">
            <el-tag :type="formatStatus(scope.row.status).type">{{
              formatStatus(scope.row.status).text
            }}</el-tag>
            <!-- <el-tag v-else-if="scope.row.status === 1" type="info"
              >待审核</el-tag
            >
            <el-tag v-else-if="scope.row.status === 2" type="success"
              >审核通过</el-tag
            >
            <el-tag v-else-if="scope.row.status === 3" type="warning"
              >审核失败</el-tag
            >
            <el-tag v-else-if="scope.row.status === 4" type="danger"
              >已删除</el-tag
            > -->
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              size="mini"
              circle
              @click="handleEdit(scope.$index, scope.row)"
            ></el-button>
            <el-button
              icon="el-icon-delete"
              size="mini"
              circle
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="pages"
        background
        layout="prev, pager, next, total"
        :total="total"
        @current-change="pageChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticles, getArticleChannels, delArticle } from '@/api/article'
// 文章状态
const articleStatus = [
  {
    id: null,
    label: '全部'
  },
  {
    id: 0,
    label: '草稿'
  },
  {
    id: 1,
    label: '待审核'
  },
  {
    id: 2,
    label: '审核通过'
  },
  {
    id: 3,
    label: '审核失败'
  },
  {
    id: 4,
    label: '已删除'
  }

]
export default {
  data () {
    return {
      articleStatus,
      // 文章总条数
      total: 0,
      // 当前页码
      curPage: 1,
      // 表单数据=》文章过滤条件
      filter: {
        // 文章状态
        status: null,
        // 文章所属频道
        channel_id: null,
        // 日期区间
        date: null,
        begin_pubdate: '', // 起始时间
        end_pubdate: ''// 截止时间
      },
      // 频道数据
      channels: [],
      // 文章列表
      articles: []
    }
  },
  computed: {
    isShowDel () {
      return this.filter.status === 0
    }
  },
  created () {
    this.getArticles()
    this.getArticleChannels()
  },
  methods: {
    // 切换页面
    pageChange (curPage) {
      // console.log(curPage)
      this.curPage = curPage
      this.getArticles()
    },
    // 查询文章
    onSearch () {
      console.log(this.filter)
      this.getArticles()
    },
    // 编辑文章
    handleEdit (index, row) {
      // console.log(index, row)
      this.$router.push('/editArticle/' + row.id.toString())
    },
    // 删除文章
    handleDelete (index, row) {
      console.log(index, row)
      this.$confirm('确定删除文章吗？', '提示', {
        type: 'warning'
      }).then(async () => {
        try {
          await delArticle(row.id.toString())
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } catch (error) {
          this.$message({
            type: 'error',
            message: '删除失败!'
          })
        }
      }).catch(() => { })
    },
    // 获取文章频道
    async getArticleChannels () {
      const { status, data: { data } } = await getArticleChannels()
      if (status === 200) {
        // console.log(data)
        this.channels = data.channels
      }
    },
    // 获取文章列表
    async getArticles () {
      // 处理后台需要的时间格式
      if (this.filter.date) {
        this.filter.begin_pubdate = this.filter.date[0]
        this.filter.end_pubdate = this.filter.date[1]
      }
      const { status, data: { data } } = await getArticles({
        channel_id: this.filter.channel_id,
        page: this.curPage,
        begin_pubdate: this.filter.begin_pubdate,
        end_pubdate: this.filter.end_pubdate,
        status: this.filter.status
      })
      // console.log(status, data)
      if (status === 200) {
        this.total = data.total_count
        this.articles = data.results
      }
    },
    // console table
    show (t) {
      console.log(t)
    },
    formatStatus (status) {
      const statusObj = [
        { text: '草稿', type: 'info' },
        { text: '待审核', type: 'info' },
        { text: '审核通过', type: 'success' },
        { text: '审核失败', type: 'warning' },
        { text: '已删除', type: 'danger' }
      ]
      return statusObj[status]
    }
  }
}
</script>

<style lang="less" scoped>
.filter-card {
  margin-bottom: 10px;
}
.pages {
  margin-top: 20px;
}
</style>
